package cn.lxm.samples.auth_server.config.custom.bussiness;

import cn.lxm.samples.auth_server.config.support.OAuth2Service;
import cn.lxm.samples.auth_server.domain.security.SecurityDetail;
import cn.lxm.samples.auth_server.domain.security.TokenDetail;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author LXM
 * @Title: oauth2
 * @Description:
 * @date 2018/8/14上午11:33
 */
@Component
public class MySavedRequestAwareAuthenticationSuccessHandler
        extends SimpleUrlAuthenticationSuccessHandler {

    Logger logger = LoggerFactory.getLogger(MySavedRequestAwareAuthenticationSuccessHandler.class);

    private RequestCache requestCache = new HttpSessionRequestCache();

    @Autowired
    private Gson gson;

    @Autowired
    private OAuth2Service oAuth2Service;

    @Override
    public void onAuthenticationSuccess(
            HttpServletRequest request,
            HttpServletResponse response,
            Authentication authentication)
            throws ServletException, IOException {

        SavedRequest savedRequest
                = requestCache.getRequest(request, response);

        String username = request.getParameter("username");
        String password = request.getParameter("password");
//        String authorization=request.getHeader("Authorization");
        String token = oAuth2Service.getToken(username, password);
        logger.debug(token);
        TokenDetail detail = gson.fromJson(token, TokenDetail.class);
        Cookie cookie = new Cookie("token", detail.getAccess_token());
        Cookie refreshToken = new Cookie("refresh_token", detail.getRefresh_token());
        response.addCookie(cookie);
        response.addCookie(refreshToken);
        response.sendRedirect("http://127.0.0.1:8081/member/list");
//
//        if (savedRequest == null) {
//            clearAuthenticationAttributes(request);
//            return;
//        }
//        String targetUrlParam = getTargetUrlParameter();
//        if (isAlwaysUseDefaultTargetUrl()
//                || (targetUrlParam != null
//                && StringUtils.hasText(request.getParameter(targetUrlParam)))) {
//            requestCache.removeRequest(request, response);
//            clearAuthenticationAttributes(request);
//            return;
//        }
//
//        clearAuthenticationAttributes(request);

    }

    public void setRequestCache(RequestCache requestCache) {
        this.requestCache = requestCache;
    }


}
